package ru.nsu.fit.client.core.commands;

import ru.nsu.fit.client.core.log_manager.realization.KernelLogManager;
import ru.nsu.fit.client.core.response.KernelResponse;
import ru.nsu.fit.client.core.soap.registration.Registration;
import ru.nsu.fit.client.core.soap.registration.RegistrationOutput;

public class RegistrationCommand implements  IClientCommand {
    private  final String login;
    private  final String password;
    private final String serverUrl;

    public RegistrationCommand(final String serverUrl, final String login, final String password)
    {
        this.login = login;
        this.password = password;
        this.serverUrl = serverUrl;
    }

    @Override
    public KernelResponse execute() {
        try
        {
            KernelLogManager.print("Registration...");
            RegistrationOutput output = Registration.execute(serverUrl, login, password);

            KernelLogManager.print("Registration - got response!");
            KernelResponse response = new KernelResponse(output.isSuccessful());
            response.setErrorText(output.getErrorMessage());

            return response;
        }
        catch (Exception e)
        {
            KernelLogManager.printf("Registration - web service error: %s", e.getMessage());
            KernelResponse response = new KernelResponse(false);
            response.setErrorText(e.getMessage());
            return response;
        }
    }
}
